package com.jeecms.wicool.dao.groupBuy.impl;

import com.google.common.base.Strings;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.groupBuy.GroupBuyDao;
import com.jeecms.wicool.entity.groupBuy.GroupBuy;
import org.joda.time.DateTime;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;

/**
 * Created by cuiyanlei on 15/6/10.
 */
@Repository
public class GroupBuyDaoImpl extends HibernateBaseDao<GroupBuy, Integer> implements GroupBuyDao {
    @Override
    protected Class<GroupBuy> getEntityClass() {
        return GroupBuy.class;
    }

    @Override
    public GroupBuy save(GroupBuy bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public GroupBuy deleteById(Integer id) {
        GroupBuy bean = get(id);
        if (bean != null) {
            getSession().delete(bean);
        }
        return bean;
    }

    @Override
    public GroupBuy findById(Integer id) {
        return get(id);
    }

    @Override
    public Pagination getPage(Integer productId, String content, int pageNo, int pageSize) {
        Finder f = Finder.create(" select bean from GroupBuy bean where 1=1 ");
        if (productId != null) {
            f.append(" and bean.product.id=:product_id").setParam("product_id", productId);
        }
        if (!Strings.isNullOrEmpty(content)) {
            f.append(" and bean.content like :content").setParam("content", "%" + content + "%");
        }

        f.append(" order by bean.updateTime desc");

        return find(f, pageNo, pageSize);
    }
}
